﻿using System.Collections.Generic;
using Rated.Website.Exceptions;
using Rated.DAL.Repository;
using Rated.DAL.Db;
using System.Linq;
using System.Web.Security;
using Rated.Website.Helpers;
using Rated.Website.Classes;

namespace Rated.Website.Models
{
    public class ViewPageViewModel
    {
        public RatedItem Item { get; private set; }
        public bool CurrentUserOwnsRatedItem { get; private set; }
        public BreadcrumbTrail BreadcrumbTrail { get; private set; }
        public ViewPageViewModel() {
            throw new EIncorrectConstructor("Incorrect Constructor");
        }
        public ViewPageViewModel(int entityid)
        {
            Item = RatedItemRepository.Get(entityid);
            var ratedItemOwnerId = Item.OwnerID;
            var currentUser = UserHelper.Current;
            var currentUserId = currentUser != null ? (int)currentUser.ProviderUserKey : -1;
            CurrentUserOwnsRatedItem = ratedItemOwnerId == currentUserId;
            foreach (var review in Item.Reviews)
                review.CurrentUserOwnsRatedItem = CurrentUserOwnsRatedItem;
            BreadcrumbTrail = new BreadcrumbTrail(Item);
        }
    }
}